<?php

include_once 'questionario.php';

class Model {

	var $db = null;
	var $user = null;
	var $pass = null;

	public function __construct($dbname , $user , $pass){
		try {
			$this->db = new PDO('mysql:host=localhost;dbname='.$dbname, $user, $pass,
				array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	public function checkUsedCode($object){
		try {
			$sql = "SELECT count(*)  FROM questionario where codice=?";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(1, $object->codice);
			$stmt->execute();
			$count=0;
			if( $row = $stmt->fetch( PDO::FETCH_BOTH ) ) {
				$count=$row[0];
			}
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
		if($count>0) return TRUE;
		else return FALSE;
	}
	// TODO generalizzare tabella oggetto
	public function insertUpdate($object){
		try {
			$sql ="INSERT INTO questionario ( ";
			foreach ($object as $k => $v) {
				if($k=='pkid'||$k=='ts_ins')
				continue;
				$sql .= " ".$k.",";
			}
			$sql = substr($sql,0,-1);
			$sql .=" )  VALUES ( ";
			foreach ($object as $k => $v) {
				if($k=='pkid'||$k=='ts_ins')
				continue;
				$sql .=" :".$k.",";
			}
			$sql = substr($sql,0,-1);
			$sql .=" ) ";
			//print $sql;
			// prepared statement
			$stm = $this->db->prepare($sql);
			// bind values
			foreach ($object as $k => $v) {
				if($k=='pkid'||$k=='ts_ins')
				continue;
				if(is_numeric($v)){
					$stm->bindParam(':'.$k,intval($v),PDO::PARAM_INT);
				}else{
					$stm->bindParam(':'.$k, strval($v),PDO::PARAM_STR);
				}
			}
			$stm->execute();
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}

	public function read($keys){
		// TODO
	}

	public function close(){
		$this->db=null;
	}

	public function exportCsv(){
		try {
			$sql ="select * from questionario";
			$quest = new questionario();
			$labels = $quest->getLabels();
			$result = $this->db->query($sql);
			$line  = "";
			foreach ($labels as $k =>$v) {
				$line .= $v.";";
			}
			print $line."\n";
			foreach ($result->fetchAll(PDO::FETCH_CLASS,"questionario") as $row) {
				$line  = "";
				foreach ($row as $k =>$v){
					$line .= $v.";";
				}
				$line = substr($line,0,-1);
				print $line."\n";
			}
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}

	public function statistica($var,$table){
		$quest = new questionario();
		$res = array();
		try {
			$stat = array();
			$sql ="select valore , count(".$var.") as cont  from questionario  "
			."join tabella on tipo='".$table."' and chiave=".$var." group by ".$var;
			$result = $this->db->query($sql);
			//print $sql;
			foreach ($result->fetchAll(PDO::FETCH_ASSOC) as $row) {
				$stat[$row['valore']]= htmlspecialchars($row['cont']);
			}

		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
		$res['titolo']=$quest->getLabel($var); 
		$res['stat']=$stat; 
		return $res;
	}

}

?>